// pages/advice/advice.ts
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenArr: ["一饭一楼", "一饭三楼", "二饭", "三饭"],
    pickerIndex: 0,
    postText: "",
    showWhat: true,
    adviceArr: [],
    pageNo: 1,
    disPicker: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setNavigationBarTitle({
      title: '悦食生活-建议或投诉',
    })
    if (app.globalData.userInfo.permission > 1) {
      if (app.globalData.userInfo.permission == 2) {
        this.setData({
          disPicker: false,
          pickerIndex: 0,
          showWhat: false,
        })
      } else {
        this.setData({
          showWhat: false,
          pickerIndex: parseInt(app.globalData.userInfo.permission) - 3
        })
      }
      this.getAdvice();
    } else if (app.globalData.userInfo.permission == 1) {
      this.setData({
        disPicker: false
      })
    }
  },
  textChange(e) {
    this.setData({
      postText: e.detail.value
    })
  },
  postAdvice() {
    if (this.data.postText != "") {
      wx.request({
        url: app.globalData.apiUrl + '/advice/addUserAdvice',
        method: "POST",
        data: {
          "token": wx.getStorageSync('token'),
          "canteenId": parseInt(this.data.pickerIndex) + 1,
          "adviceText": this.data.postText
        },
        success: (res) => {
          console.log(res);
          if (res.data.code == 20001) {
            wx.showToast({
              title: '提交成功!',
            })
            this.setData({
              postText: ""
            })
          }

        }
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '内容不能为空',
        showCancel: false,
        complete: (res) => {}
      })
    }

  },
  getAdvice() {
    if (app.globalData.userInfo.permission > 1) {
      wx.request({
        url: app.globalData.apiUrl + '/advice/getCanteenAdviceList',
        method: "POST",
        data: {
          "token": wx.getStorageSync('token'),
          "canteenId": parseInt(this.data.pickerIndex) + 1,
          "pageNo": this.data.pageNo,
          "pageSize": 20
        },
        success: (res) => {
          console.log(res.data.data.rows);
          let newArr = res.data.data.rows;
          for (let index = 0; index < newArr.length; index++) {
            // console.log(newCommentArr[index].commentDate.slice(0,10));
            //截取时间的前几位
            newArr[index].createTime = newArr[index].createTime.slice(0, 10)
          }
          this.setData({
            adviceArr: newArr
          })
        }
      })
    }
  },
  continueGetAdvice() {
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    wx.request({
      url: app.globalData.apiUrl + '/advice/getCanteenAdviceList',
      method: "POST",
      data: {
        "token": wx.getStorageSync('token'),
        "canteenId": parseInt(this.data.pickerIndex) + 1,
        "pageNo": this.data.pageNo,
        "pageSize": 20
      },
      success: (res) => {
        console.log(res.data.data.rows);
        let newArr = res.data.data.rows;
        for (let index = 0; index < newArr.length; index++) {
          // console.log(newCommentArr[index].commentDate.slice(0,10));
          //截取时间的前几位
          newArr[index].createTime = newArr[index].createTime.slice(0, 10)
        }
        let oldArr = this.data.adviceArr
        let newArrOne = oldArr.concat(newArr)
        this.setData({
          adviceArr: newArrOne
        })
      }
    })
  },
  pickerChange(e) {
    this.setData({
      pickerIndex: e.detail.value
    })
    if(app.globalData.userInfo.permission>1){
      this.getAdvice();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.continueGetAdvice();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})